<?php
require_once "DatabaseConnection.php";
require_once "DbParams.php";
require_once "Record.php";
class ScoresUtils {
    private $connection;

    public function __construct(){
        $this->connection = new DatabaseConnection(DbParams::$db_host, DbParams::$db_user, DbParams::$db_pass, DbParams::$db_name, DbParams::$db_port);
        //mysqli_set_charset($this->connection, "utf8");
        //mysqli_query($this->connection, "set names utf8");
    }

    public function addNewGame($name, $hash){
        $source = "INSERT INTO `games`(`name`, `hash`) VALUES (\"%s\", \"%s\");";
        $query = sprintf($source, $name, $hash);
        $result = mysqli_query($this->connection, $query);

        return $result;
    }

    public function getScores($name){
        $source = "SELECT * FROM `scores` WHERE `scores`.`game_id` = (SELECT `games`.`id` FROM `games` WHERE `games`.`name` = \"%s\") ORDER BY `scores`.`value` DESC;";
        $query = sprintf($source, $name);
        $result = mysqli_query($this->connection, $query) or die(mysqli_error($this->connection));
        $rn = mysqli_num_rows($result);
        $records = array();
        for ($i = 0; $i < $rn; $i++){
            $f = mysqli_fetch_array($result);
            $r = new Record();
            $r->userName = $f[user_name];
            $r->scores = (int) $f[value];
            $r->levels = (int) $f[levels];
            $r->stars = (int) $f[stars];
            $records[$i] = $r;
        }

        return json_encode($records);
    }

    public function addScores($data){
        if (!$this->checkTheUser($data["user_name"], $data["game_name"])){
            $game_name = strlen($data["game_name"]) != 0 ? $data["game_name"] : "empty";
            $user_name = strlen($data["user_name"]) != 0 ? $data["user_name"] : "Anonymous";
            $levels = strlen($data["levels"]) != 0 ? $data["levels"] : 0;
            $stars = strlen($data["stars"]) != 0 ? $data["stars"] : 0;
            $scores_value = strlen($data["scores_value"]) != 0 ? $data["scores_value"] : 0;
            $source = "INSERT INTO `scores`(`value`, `levels`, `stars`, `user_name`, `game_id`) VALUES (%d,%d,%d,\"%s\",(SELECT `games`.`id` FROM `games` WHERE `games`.`name` = \"%s\"));";
            $query = sprintf($source, $scores_value, $levels, $stars, $user_name, $game_name);
            $result = mysqli_query($this->connection, $query);
            if ($result){
                echo "ok";
            }
        } else {
            echo "user exists!";
        }
    }

    public function updateScores($data){
        $game_name = strlen($data["game_name"]) != 0 ? $data["game_name"] : "empty";
        $user_name = strlen($data["user_name"]) != 0 ? $data["user_name"] : "Anonymous";
        $levels = strlen($data["levels"]) != 0 ? $data["levels"] : 0;
        $stars = strlen($data["stars"]) != 0 ? $data["stars"] : 0;
        $scores_value = strlen($data["scores_value"]) != 0 ? $data["scores_value"] : 0;
        $source = "UPDATE `scores` SET `value` = %d, `levels` = %d, `stars` = %d WHERE `scores`.`game_id` = (SELECT `games`.`id` FROM `games` WHERE `games`.`name` = \"%s\") AND `scores`.`user_name` = \"%s\";";
        $query = sprintf($source, $scores_value, $levels, $stars, $game_name, $user_name);
        $result = mysqli_query($this->connection, $query);
        if ($result){
            echo "ok";
        } else {
            echo "fuck";
        }
    }

    public function checkTheUser($name, $game){
        $source = "SELECT `scores`.`value` FROM `scores` WHERE `scores`.`game_id` = (SELECT `games`.`id` FROM `games` WHERE `games`.`name` = \"%s\") AND `scores`.`user_name` = \"%s\";";
        $query = sprintf($source, $game, $name);
        $result = mysqli_query($this->connection, $query) or die(mysqli_error($this->connection));
        $rows = mysqli_num_rows($result);
        if ($rows > 0){
            return true;
        }

        return false;
    }
}